package com.dsiab.manageService.controller;

import com.dsiab.manageService.common.ResponseMap;
import com.dsiab.manageService.entity.*;
import com.dsiab.manageService.mail.MailSenderBoot;
import com.dsiab.manageService.service.AccountService;
import com.dsiab.manageService.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

  @Autowired
  MailSenderBoot mailSenderBoot;
  @Autowired
  AccountService accountService;
  @Autowired
  AdminService adminService;

  /**
   * 文章详情
   * @param id
   * @return
   */
  @GetMapping("/getDetail")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getOne(@RequestParam String id) {
    return ResponseMap.success(adminService.getDetail(id));
  }

  /**
   * 文章分页列表
   */
  @PostMapping("/getList")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getList(@RequestBody PagesDto PagesDto) {
    return ResponseMap.success(adminService.getListByCatePage(PagesDto));
  }

  /**
   * 查询所有分类
   */
  @GetMapping("/getCate")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getCate() {
    return ResponseMap.success(adminService.getCateList());
  }

  /**
   * 新增文章
   */
  @PostMapping("/insertItem")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap insertItem(@RequestBody PagesDto pagesDto) {
    // 新增成功后返回当前插入的id用于自动保存
    if ( "".equals(pagesDto.getCate())){
      return ResponseMap.error("请选择分类");
    }
    if ( "".equals(pagesDto.getTitle())){
      return ResponseMap.error("请输入标题");
    }
    if ( "".equals(pagesDto.getContent())){
      return ResponseMap.error("请输入内容");
    }
    int id = adminService.insertOne(pagesDto);
    return ResponseMap.success(id);
  }

  /**
   * 删除文章
   */
  @PostMapping("/deleteItem")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap delOne(@RequestBody PagesDto PagesDto) {
    adminService.delOne(PagesDto.getId());
    return ResponseMap.success();
  }
  /**
   * 批量删除文章
   */
  @PostMapping("/batchDelPost")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap batchDelPost(@RequestBody Map map) {
    adminService.batchDelPost((List)map.get("ids"));
    return ResponseMap.success();
  }

  /**
   * 更新文章
   */
  @PostMapping("/updateItem")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap updateOne(@RequestBody PagesDto pagesDto) throws Exception {
    // 更新成功后返回当前插入的id用于自动保存
    int id = adminService.updateOne(pagesDto);
    return ResponseMap.success(id);
  }

  /**
   * 新增文章分类
   */
  @PostMapping("/insertCate")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap inertOne(@RequestBody CateDto cateDto) {
    adminService.insertCate(cateDto);
    return ResponseMap.success();
  }

  /**
   * 更新文章分类
   */
  @PostMapping("/updateCate")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap updateCate(@RequestBody CateDto cateDto) {
    adminService.updateCate(cateDto);
    return ResponseMap.success();
  }

  /**
   * 删除文章分类
   */
  @PostMapping("/delCate")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap delCate(@RequestBody CateDto cateDto) {
    adminService.delCate(cateDto.getId());
    return ResponseMap.success();
  }

  /**
   * 查询所有上传文件
   */
  @GetMapping("/getSourceList")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getSourceList() {
    Map siteInfo = accountService.getSiteInfo();
    String realPath = (String) siteInfo.get("sourceRealUrl");
    File dir =new File(realPath);//浏览F盘a文件夹下的所有内容
    // File dir =new File("/Users/xxn/Downloads/");//浏览F盘a文件夹下的所有内容
    File[] files=dir.listFiles();   //列出所有的子文件
    ArrayList arrayList = new ArrayList();
    for(File file :files) {
      if(file.isFile()){
        //如果是文件，则输出文件名字
        Map<String, Object> itemMap = new HashMap<>();
        itemMap.put("filename", file.getName());
        arrayList.add(itemMap);
      }else if(file.isDirectory()) {
        // 如果是文件夹，则输出文件夹的名字，并递归遍历该文件夹
        // System.out.println(file.getName());
      }
    }
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("result", arrayList);
    resultMap.put("sourceUrl", (String)siteInfo.get("sourceUrl"));
    return new ResponseMap(0, "更新成功", resultMap);
  }

  /**
   * 删除文件
   */
  @PostMapping("/delFile")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delFile(@RequestBody Map fileInfo) {
    Map siteInfo = accountService.getSiteInfo();
    String realPath = (String) siteInfo.get("sourceRealUrl");
    File file = new File(realPath + fileInfo.get("filename"));
    if (file.exists()) {
      file.delete();
      return new ResponseMap(0, "更新成功", file);
    }
    return new ResponseMap(1, "更新失败", file);
  }
  /**
   * 发送邮件
   */
  @PostMapping("/sendMail")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap sendMail(@RequestBody Map map) {
    mailSenderBoot.sendMail((String) map.get("mailTo") ,(String)map.get("content"), (String)map.get("title"));
    return ResponseMap.success();
  }

  /**
   * 查询用户列表
   */
  @GetMapping("/getUserList")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getUserList(@RequestParam String pageNo) {
    return ResponseMap.success(adminService.getUserListByPage(pageNo));
  }

  /**
   * 查询角色列表
   */
  @PostMapping("/getRoleList")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getRoleList() {
    return ResponseMap.success(adminService.getRoleList());
  }

  /**
   * 更新用户角色
   */
  @PostMapping("/updateUserRole")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateRole(@RequestBody Map<String,Object> roleMap) {
    adminService.updateRole(roleMap);
    return ResponseMap.success();
  }

  /**
   * 删除用户
   */
  @PostMapping("/deleUser")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deleUser(@RequestBody Map map) {
    accountService.deleUser( (int)map.get("id"), (String) map.get("username"));
    return ResponseMap.success();
  }

  /**
   * 查询站点信息
   */
  @PostMapping("/getSiteInfo")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getSiteInfo() {
    return ResponseMap.success(accountService.getSiteInfo());
  }

  /**
   * 清除缓存
   */
  @PostMapping("/clearCache")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap clearCache() {
    adminService.clearCache();
    return ResponseMap.success();
  }

  /**
   * 新增专题
   */
  @PostMapping("/insertTopic")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap insertTopic(@RequestBody TopicDto topicDto) {
    adminService.insertTopic(topicDto);
    return ResponseMap.success();
  }

  /**
   * 更新专题名称
   */
  @PostMapping("/updateTopic")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateTopic(@RequestBody TopicDto topicDto) {
    adminService.updateTopic(topicDto);
    return ResponseMap.success();
  }

  /**
   * 查询专题列表
   */
  @GetMapping("/getTopicList")
  public ResponseMap getTopic() {
    return ResponseMap.success(adminService.getTopicList());
  }

  /**
   * 添加文章到专题
   */
  @PostMapping("/addPostToTopic")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap addPostToTopic(@RequestBody TopicDto topicDto) {
    adminService.addPostToTopic(topicDto);
    return ResponseMap.success();
  }

  /**
   * 查询专题详情列表
   */
  @PostMapping("/getTopicDetailList")
  public ResponseMap getTopicDetailList(@RequestBody TopicDto topicDto) {
    return ResponseMap.success(adminService.getTopicDetailList(topicDto));
  }

  /**
   * 删除专题
   */
  @PostMapping("/deleteTopic")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deleteTopic(@RequestBody TopicDto topicDto) {
    adminService.deleteTopic(topicDto);
    return ResponseMap.success();
  }

  /**
   * 删除专题文章
   */
  @PostMapping("/deleteTopicDetail")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deleteTopicDetail(@RequestBody TopicDto topicDto) {
    adminService.deleteTopicDetail(topicDto);
    return ResponseMap.success();
  }

  /**
   * 查询回收站文章
   */
  @PostMapping("/getRecycleList")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getRecycleList(@RequestBody PagesDto PagesDto) {
    return ResponseMap.success(adminService.getRecycleListByCatePage(PagesDto));
  }

  /**
   * 清空回收站文章
   */
  @PostMapping("/deletePost")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deletePost(@RequestBody Map map) {
    List ids = (List)map.get("ids");
    if (ids.size()==0){
      return ResponseMap.error("请选择数据");
    }
    adminService.deletePost(ids);
    return ResponseMap.success();
  }

  /**
   * 新增菜单
   */
  @PostMapping("/insertMenu")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap insertMenu(@RequestBody MenuDto menuDto) {
    adminService.insertMenu(menuDto);
    return ResponseMap.success();
  }

  /**
   * 查询所有菜单
   */
  @GetMapping("/getMenuList")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getMenuList(MenuDto menuDto) {
    return ResponseMap.success(adminService.getMenuList(menuDto));
  }

  /**
   * 更新菜单
   */
  @PostMapping("/updateMenu")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateMenu(@RequestBody MenuDto menuDto) {
    adminService.updateMenu(menuDto);
    return ResponseMap.success();
  }

  /**
   * 删除菜单
   */
  @PostMapping("/deleteMenu")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap deleteMenu(@RequestBody MenuDto menuDto) {
    adminService.deleteMenu(menuDto);
    return ResponseMap.success();
  }

  /**
   * 查询登录用户菜单
   */
  @GetMapping("/getMenus")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap getMenus() {
    return ResponseMap.success(adminService.getMenus());
  }

  /**
   *  查询所有菜单，用户管理员配置菜单
   */
  @GetMapping("/getAllMenus")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getAllMenus() {
    return ResponseMap.success(adminService.getAllMenus());
  }

  /**
   * 更新角色名称以及对应菜单
   */
  @PostMapping("/updateRoleName")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap updateRoleName(@RequestBody Map map) {
    adminService.updateRoleName(map);
    return ResponseMap.success("保存成功");
  }

  /**
   * 查询用户角色菜单
   */
  @GetMapping("/getMenuByRole")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap getMenuByRole(@RequestParam int roleId) {
    return ResponseMap.success(adminService.getMenuByRole(roleId));
  }

  /**
   * 更新密码
   */
  @PostMapping("/updatePwd")
  @PreAuthorize("hasRole('ROLE_author')")
  public ResponseMap updatePwd(@RequestBody Map pwdMap) throws Exception {
    // 更新成功后返回当前插入的id用于自动保存
    adminService.updatePwd(pwdMap);
    return ResponseMap.success();
  }

  /**
   * 查询所有评论
   */
  @PostMapping("/getAllComment")
  public ResponseMap getAllComment(@RequestBody CommentsDto commentsDto) {
    return ResponseMap.success(adminService.getAllComment(commentsDto));
  }

  /**
   * 删除评论
   */
  @PostMapping("/delComment")
  @PreAuthorize("hasRole('ROLE_admin')")
  public ResponseMap delComment(@RequestBody Map map) {
    adminService.delComment((List) map.get("ids"));
    return ResponseMap.success();
  }

  /**
   * 查询标签分页列表
   */
  @GetMapping("/getListByTagsPage")
  public ResponseMap getListByTagsPage(@RequestParam String tag, String page){
    return ResponseMap.success(adminService.getListByTagsPage(tag,page));
  }
  @PostMapping("/approveComment")
  public ResponseMap approveComment(@RequestBody CommentsDto commentsDto){
    adminService.approveComment(String.valueOf(commentsDto.getId()));
    return ResponseMap.success("审核成功");
  }

  /**
   * 回收站恢复文章
   * @param map
   * @return
   */
  @PostMapping("/recoveryPost")
  public ResponseMap recoveryPost(@RequestBody Map map){
    adminService.recoveryPost((String)map.get("uid"));
    return ResponseMap.success("恢复成功");
  }
}
